package com.financing.wap.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.common.Views;
import com.financing.wap.util.HttpRequestDeviceUtils;


public class BaseController {
	
	/**
	 * 日志类
	 */
	protected static Logger log = LoggerFactory.getLogger(BaseController.class);
	
	/**
	 * 视图持有声明
	 */
	protected static final Views views = Views.getInstance();
	
	/**
	 * 请求跳转
	 * @param url
	 * @return
	 */
	protected String redirct(String url){
		return "redirect:" + url;
	}
	
	/**
	 * 获取系统session
	 * @return
	 */
	protected static Session getSession(){
		return SessionUtil.getSession();
	}
	
	/**
	 * 获取系统session
	 * @return
	 */
	protected static Session getSession(boolean create){
		return SessionUtil.getSession(create);
	}
	
	/**
	 * 设置系统session
	 * @param session
	 */
	protected void setSession(Session session){
		SessionUtil.setSession(session);
	}
	
	/**
	 * 获取容器底层session
	 * @return
	 */
	protected static HttpSession getHttpSession(){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request.getSession(true);
	}
	
	/**
	 * 获取HttpRequest
	 * @return
	 */
	protected static HttpServletRequest getHttpRequest(){
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}

	protected String getTplPath(HttpServletRequest request){
		return HttpRequestDeviceUtils.isMobileDevice(request) ? "" : "pc/";
	}
	
}
